
<template>
  <el-form :inline="true" :model="formIn" class="demo-form-inline">
    <!-- 标题配置栏 -->
    <el-form-item label="角色名称">
      <el-input
        v-model="formIn.roleName"
        placeholder="请输入角色名称"
      ></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onsell" icon="el-icon-search"
        >查询</el-button
      >
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="cz" icon="el-icon-female"
        >重置</el-button
      >
    </el-form-item>
    <el-form-item>
      <el-button
        icon="el-icon-circle-plus-outline"
        type="primary"
        @click="jumpAddRolePage"
        >新增</el-button
      >
    </el-form-item>

    <!-- 表格 -->
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="角色编号" hidden="true" />
      <el-table-column prop="roleCode" label="角色编码" />
      <el-table-column prop="roleName" label="角色名称" />
      <el-table-column prop="createUser" label="创建人" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column prop="updateTime" label="修改时间" />
      <el-table-column prop="remark" label="备注" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="jumpEditRolePage(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="deleteRole(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="新增角色" :visible.sync="dialogFormVisible">
      <el-form :model="addroleform">
        <el-form-item label="角色编码" :label-width="formLabelWidth">
          <el-input
            v-model="addroleform.roleCode"
            autocomplete="true"
          ></el-input>
        </el-form-item>

        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input
            v-model="addroleform.roleName"
            autocomplete="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="addroleform.remark" autocomplete="true"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRole">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑角色" :visible.sync="dialogFormVisible1">
      <el-form :model="editroleform">
        <el-form-item label="角色编码" :label-width="formLabelWidth">
          <el-input
            v-model="editroleform.roleCode"
            autocomplete="true"
          ></el-input>
        </el-form-item>

        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input
            v-model="editroleform.roleName"
            autocomplete="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
            v-model="editroleform.remark"
            autocomplete="true"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="editRole">确 定</el-button>
      </div>
    </el-dialog>
  </el-form>
</template>


<script>
import {
  getAllRoles,
  getRoleByRoleName,
  addRole,
  selectRoleDeatil,
  deleteRole,
  editRole,
} from "@/api/role";

export default {
  name: "department",
  data() {
    return {
      formIn: {
        roleName: "",
      },
      departmentform: {
        departmentName: "",
        phone: "",
        address: "",
        delivery: false,
      },
      editdepartmentform: {
        departmentName: "",
        phone: "",
        address: "",
        delivery: false,
      },
      tableData: [],
      options: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      dialogFormVisible1: false,

      formLabelWidth: "120px",
      addroleform: {},
      editroleform: {},
      selectLabel: "",
      selectValue: "",
    };
  },

  created() {
    this.selectall();
  },
  methods: {
    async onsell() {
      const likesell = {
        roleName: this.formIn.roleName,
      };
      console.log("根据角色名称查询角色,", likesell);
      let res = await getRoleByRoleName(likesell);
      this.tableData = res.data;
    },
    async selectall() {
      //查询全部数据
      let res = await getAllRoles();
      console.log("查询全部角色数据_________res,", res);
      this.tableData = res.data;
    },

    async cz() {
      this.formIn.roleName = null;
    },
    async editRole() {
      const gou = { ...this.editroleform };
      await editRole(gou);

      this.dialogFormVisible1 = false;
      this.selectall();
    },
    async jumpAddRolePage() {
      this.dialogFormVisible = true;
    },
    async jumpEditRolePage(index, row) {
      const params = {
        id: row.id,
        roleCode: row.roleCode,
      };
      const res = await selectRoleDeatil(params);
      this.editroleform = res.data;
      this.dialogFormVisible1 = true;
    },

    async deleteRole(index,row) {
      console.log("删除的数据是_________index,", index);

      console.log("删除的数据是_________row,", row);
      const params = {
        id:row.id,
      }
      await deleteRole(params);
      this.selectall();
    },
    async addRole() {
      this.dialogFormVisible = false;
      const yang = { ...this.addroleform };
      console.log("新增的数据是_________yang,", yang);
      await addRole(yang);
      this.selectall();
    },
  },
};
</script>

<style lang="sess">
</style>
